package com.gzsxy.esjy.gateway.controller;


import com.gzsxy.esjy.service.base.dto.request.UserRequest;
import com.gzsxy.esjy.service.base.dto.response.DefaultResponse;
import com.gzsxy.esjy.gateway.config.CustomRouteRepository;
import com.gzsxy.esjy.gateway.dto.LoginDto;
import com.gzsxy.esjy.gateway.service.GlobalService;
import com.gzsxy.esjy.gateway.sso.dto.DefaultUser;
import com.gzsxy.esjy.redis.user.ISystemUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ServerWebExchange;

import javax.validation.constraints.NotBlank;

/**
 * @author xiaolong
 * @version 1.0
 * @description:
 * @date 2021/11/12 19:48
 */

@RestController
@RequestMapping("/global")
@Api("系统登录、权限操作")
@Slf4j
public class GlobalController {


    @Autowired
    private GlobalService globalService;

    @Autowired
    private CustomRouteRepository customRouteRepository;


    /**
     * @description: 登录准备
     * @return
     */
    @GetMapping("/login.ready")
    @ApiOperation(value = "登录-请求用户名", notes = "登录-请求用户名")
    public DefaultResponse loginReady(@Validated @NotBlank(message = "username不能为空") String username,
                                      @Validated @NotBlank(message = "loginId不能为空") String loginId){
        return DefaultResponse.ok(globalService.loginReady(username, loginId));
    }

    /**
     * @description: 登录
     * @return
     */
    @PostMapping("/login.pwd")
    @ApiOperation(value = "登录-请求用户名", notes = "登录-请求用户名")
    public DefaultResponse login(@Validated @RequestBody LoginDto loginDto, ServerWebExchange exchange){
        DefaultUser defaultUser = globalService.login(loginDto,exchange);
        DefaultResponse ok = DefaultResponse.ok(defaultUser);
        return ok;
    }


    /**
     * @description: 直接登录
     * @param
     * @return
     */
    @GetMapping("/login.direct")
    @ApiOperation(value = "直接登录", notes = "直接登录")
    public DefaultResponse login(@RequestParam("username") String username,
                   @RequestParam("password") String password,
                   @RequestParam(value = "loginId", required = false) String loginId,
                   ServerWebExchange exchange){
        DefaultUser user = globalService.login(username,password,loginId,exchange);
        DefaultResponse ok = DefaultResponse.ok(user);
        return ok;
    }

    @GetMapping("/logout")
    @ApiOperation(value = "退出登录",notes = "退出登录")
    public DefaultResponse logout(@Validated @NotBlank(message = "access_token 不能为空")String access_token){
        globalService.logout(access_token);
        return DefaultResponse.ok();
    }


    @GetMapping("/refresh")
    @ApiOperation(value = "刷新token",notes = "刷新token")
    public DefaultResponse logout(
            @Validated @NotBlank(message = "access_token 不能为空")String access_token,
            @Validated @NotBlank(message = "refresh_token 不能为空")String refresh_token,
            ServerWebExchange exchange){
        ISystemUser refresh = globalService.refresh(access_token, refresh_token, exchange);
        return DefaultResponse.ok(refresh);
    }

    @GetMapping("/getLoginUser")
    @ApiOperation(value = "获取登录用户",notes = "获取登录用户")
    public DefaultResponse logout(
            @Validated @NotBlank(message = "access_token 不能为空")String access_token,
            ServerWebExchange exchange){
        if (access_token == null){
            access_token = exchange.getRequest().getHeaders().getFirst("access_token");
        }
        ISystemUser loginUser = globalService.getLoginUser(access_token, exchange);
        return DefaultResponse.ok(loginUser);
    }

    @GetMapping("/refreshRoute")
    @ApiOperation(value = "刷新路由",notes = "刷新路由")
    public DefaultResponse refreshRoute() {
        customRouteRepository.refresh();
        return DefaultResponse.ok();
    }


    @GetMapping("/getUserMenu")
    @ApiOperation(value = "获取用户菜单",notes = "获取用户菜单")
    public DefaultResponse getUserMenu(@RequestParam("access_token") String access_token,
                                       @RequestParam("appCode") String appCode,
                                       ServerWebExchange exchange){
        return DefaultResponse.ok(globalService.getUserMenu(access_token,appCode,exchange));
    }





    @PostMapping("/verify")
    @ApiOperation("商家审核成功注册账户")
    public DefaultResponse register(@RequestParam("access_token") String access_token, ServerWebExchange exchange,@RequestBody UserRequest userRequest){
        if (access_token == null){
            access_token = exchange.getRequest().getHeaders().getFirst("access_token");
        }
        globalService.verify(access_token,exchange,userRequest);
        return DefaultResponse.ok();
    }

//    @Value("${config.info}")
//    private String configInfo;
//
//    @GetMapping("/configInfo")
//    public String getConfigInfo() {
//        return configInfo;
//    }



}
